#!/bin/sh

die () { echo "$@" ; cleanup ; exit 1; }

# Source nvm
\. ../../../nvm.sh

\. ../../common.sh

NVM_TEST_VERSION=v5.99.99

cleanup() {
  VERSION_PATH="${VERSION_PATH-}"
  if [ -n "${VERSION_PATH}" ] && [ -d "${VERSION_PATH}" ]; then
    sudo -n rm -f "${VERSION_PATH}/lib/root-owned-file" 2>/dev/null
    rm -rf "${VERSION_PATH}"
  fi
}

# The permissions check only trips on files that are neither writable nor
# self-owned, and creating one requires another user: skip where passwordless
# sudo is unavailable; CI always has it.
if ! sudo -n true 2>/dev/null; then
  echo 'passwordless sudo is not available, skipping'
  exit 0
fi

# Install a fake version to uninstall
make_fake_node "${NVM_TEST_VERSION}" || die 'unable to make fake node'
VERSION_PATH="$(nvm_version_path "${NVM_TEST_VERSION}")"

# Make part of the installation folder unwritable, and not self-owned
mkdir -p "${VERSION_PATH}/lib"
sudo -n touch "${VERSION_PATH}/lib/root-owned-file" || die 'sudo touch failed'
sudo -n chmod 444 "${VERSION_PATH}/lib/root-owned-file" || die 'sudo chmod failed'

# Make sure the version to uninstall is not the active one
nvm deactivate >/dev/null 2>&1 || true

# Attempt to uninstall it
RETURN_MESSAGE="$(nvm uninstall "${NVM_TEST_VERSION}" 2>&1)"
CHECK_FOR="Cannot uninstall, incorrect permissions on installation folder"
case "${RETURN_MESSAGE}" in
  *"${CHECK_FOR}"*) ;;
  *) die "Failed to show error message; got: ${RETURN_MESSAGE}" ;;
esac

# ... and the version must not have been removed
nvm_is_version_installed "${NVM_TEST_VERSION}" || die 'version was uninstalled despite the permissions error'

cleanup
